package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.Member;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.MemberService;
import net.starwifi.duobao.utils.IpUtils;
import net.starwifi.duobao.utils.WXPrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/27.
 */
@Controller("wxBindMobileController")
@RequestMapping(value = "/bind")
public class WXBindMobileController extends BaseController{

    @Autowired
    MemberService memberService;

    /**
     * 手机号绑定页面
     */
    @RequestMapping(value = "/bind-mobile", method = RequestMethod.GET)
    public String bindWX(HttpServletRequest request,String redirectUrl,Model model) {
        String wxAccessToken=(String)request.getSession().getAttribute("wxAccessToken");
        String wxOpenId=(String)request.getSession().getAttribute("wxOpenId");
        model.addAttribute("redirectUrl",redirectUrl);
        return "bind/bind-mobile";
    }


    /**
     * 手机号绑定提交
     */
    @ResponseBody
    @RequestMapping(value = "/bind-mobile-validate", method = RequestMethod.POST)
    public Message bindMobileValidate(HttpServletRequest request,String mobile,String smsCode) {
        String wxAccessToken=(String)request.getSession().getAttribute("wxAccessToken");
        String wxOpenId=(String)request.getSession().getAttribute("wxOpenId");
        Member member= memberService.findByMobile(mobile);
        if (member == null) {
            member=new Member();
            member.setUsername("m" + mobile);
            String salt= net.starwifi.duobao.utils.StringUtils.getUUID();
            member.setSalt(salt);
            member.setMobile(mobile);
            member.setIsEnabled(true);
            member.setIsLocked(false);
            member.setLoginFailureCount(0);
            member.setLockedDate(null);
            member.setBalance(BigDecimal.ZERO);
            member.setCurrencyBalance(BigDecimal.ZERO);
            member.setVirtualCurrency(BigDecimal.ZERO);
        }
        if(member.getWxOpenId()!=null){
            return Message.error("该手机号已经绑定过");
        }else{
            member.setWxOpenId(wxOpenId);
        }
        if (!member.getIsEnabled()) {
            return Message.error("请稍后再试");
        }
        if(member.getVirtualCurrency()==null){
            member.setVirtualCurrency(BigDecimal.ZERO);
        }
        member.setLoginIp(IpUtils.getIpAddr(request));
        member.setLoginDate(new Date());
        member.setLoginFailureCount(0);
        if(member.getId()==null){
            memberService.save(member);
        }else{
            memberService.update(member);
        }
        HttpSession session=request.getSession();
        Map<String, Object> attributes = new HashMap<String, Object>();
        Enumeration<?> keys = session.getAttributeNames();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            attributes.put(key, session.getAttribute(key));
        }
        session.invalidate();
        session = request.getSession();
        for (Map.Entry<String, Object> entry : attributes.entrySet()) {
            session.setAttribute(entry.getKey(), entry.getValue());
        }
        session.setAttribute(WXPrincipal.PRINCIPAL_WX_MEMBER, new WXPrincipal(member.getId(),member.getMobile(),member.getWxOpenId()));
        return Message.success("绑定成功");
    }



}
